home *** CD-ROM | disk | FTP | other *** search
- // Copyright 1992 by Jon Dart. All Rights Reserved
-
- #ifndef S_LIST_H
- #define S_LIST_H
- #include "snode.h"
-
- template <class T>
- class S_List {
- // singly-linked unordered list class
-
- public:
-
- S_List();
- virtual ~S_List();
- unsigned Length() {
- return len;
- }
- int Empty() {
- return len == 0;
- }
- void Rewind() {
- current = first;
- }
- T *First() {
- return first->Contents();
- }
- T *Last() {
- return last->Contents();
- }
- T *Current() {
- return current->Contents();
- }
- virtual T *Next();
- void Insert( const T &item );
- void Traverse( void (*func)(T *) );
-
- void *operator new(size_t size)
- {
- return allocator.allocate(size);
- }
-
- void operator delete( void *p )
- {
- allocator.free(p);
- }
-
- static void freeAll(Boolean final = False)
- {
- allocator.freeAll(final);
- }
-
- private:
- S_Node<T> *first;
- S_Node<T> *current;
- S_Node<T> *last;
- unsigned len;
- static Pool allocator;
-
- };
-
- template <class T>
- S_List<T>::S_List() {
- first = current = last = NULL;
- len = 0;
- }
-
- template <class T>
- S_List<T>::~S_List()
- {
- // don't delete the contents
- // Rewind();
- // S_Node<T> *tmp;
-
- // while (current)
- // {
- // tmp = current->Link();
- // delete current;
- // current = tmp;
- // }
- first = current = last = NULL;
- len = 0;
- }
-
- template <class T>
- T * S_List<T>::Next()
- {
- if (current == NULL)
- return NULL;
- current = current->Link();
- if (current == NULL)
- return NULL;
- return current->Contents();
- }
-
- template <class T>
- void S_List<T>::Insert( const T &item )
- {
- S_Node<T> *new_node = new S_Node<T>(item);
-
- new_node->MakeLink( NULL );
- if (last == NULL) // list is empty
- first = new_node;
- else
- last->MakeLink(new_node);
- current = last = new_node;
- len++;
- }
-
- template <class T>
- void S_List<T>::Traverse( void (*func)(T *) )
- {
- unsigned i;
-
- Rewind();
- for( i = 0; i < Length(); i++ )
- {
- (*func)( Current() );
- Next();
- }
- }
-
- #endif
-
-